﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApp32
{
    public class DrawingCanvas :Canvas
    {
        private List<DrawingVisual> visuals = new();

        protected override int VisualChildrenCount => visuals.Count;

        protected override Visual GetVisualChild(int index)
        {
            return visuals[index];
        }

        public void AddVisual(DrawingVisual visual)
        {
            visuals.Add(visual);

            //只有调用，这两个方法，才可以显示
            this.AddLogicalChild(visual);
            this.AddVisualChild(visual);
        }

        public void DeleteVisual(DrawingVisual visual)
        {
            visuals.Remove(visual);

            this.RemoveLogicalChild(visual);
            this.RemoveVisualChild(visual);
        }

        public DrawingVisual? GetDrawingVisual(Point  pointClick )
        {
            HitTestResult result = VisualTreeHelper.HitTest(this, pointClick);
            return result.VisualHit as DrawingVisual;
        }
    }
}
